{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c1e81d96",
   "metadata": {},
   "source": [
    "-------\n",
    "\n",
    "* 本周主要内容：语音识别\n",
    "* 23春_API_人工智能与机器学习_week12\n",
    "\n",
    "<br/>\n",
    "<br/>\n",
    "\n",
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b25979f0",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "## 关于麦克风包安装\n",
    "\n",
    "## 优化ASR(自动语音识别)机器人\n",
    "\n",
    "## 连接图灵机器人(不用自己的字典)\n",
    "\n",
    "## 语音合成"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af33a634",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3d2b2d61",
   "metadata": {},
   "source": [
    "# 关于麦克风包安装"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbe67cf8",
   "metadata": {},
   "source": [
    "* 安装 speech_recognition\n",
    "> pip install SpeechRecognition\n",
    "\n",
    "* 安装 PyAudio\n",
    "> Not found name \"PyAudio\" moudle\n",
    "\n",
    "> pip install pipwin\n",
    "\n",
    "> pipwin install PyAudio"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64abbd9e",
   "metadata": {},
   "source": [
    "![](\"安装openai.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d3b259e",
   "metadata": {},
   "source": [
    "!["
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb7aba7a",
   "metadata": {},
   "source": [
    "# 优化ASR(自动语音识别)机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cbd1bce7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 智超 ：百度密钥\n",
    "API_KEY = 'smxkOHWjqLVljEmIry5vuSYI'\n",
    "SECRET_KEY = 'ky3FZeSSDurxLyLLqZ2kPaEnts9NH1W1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "eccab383",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "aed5e1b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.68a300189926eef08b33d4eebc3f048e.2592000.1686666051.282335-19331335'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "xu_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f9f96919",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "41789939",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 9.273930\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'你好。'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "asr_output"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79f07c7e",
   "metadata": {},
   "source": [
    "# 连接OpenAi\n",
    "\n",
    "* 示例代码：\n",
    "```\n",
    "import openai\n",
    "#主循环\n",
    "def openai_chat(question):\n",
    "    api_key = \"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\"\n",
    "    openai.api_key = api_key\n",
    "\n",
    "    #获取用户输入的问题\n",
    "    #question = input(\"请输入问题：\")\n",
    "\n",
    "    #调用openai的api\n",
    "    completion = openai.ChatCompletion.create(\n",
    "        model=\"gpt-3.5-turbo\",\n",
    "        messages=[{\"role\": \"user\",\n",
    "                     \"content\": question}],\n",
    "        max_tokens=4000,\n",
    "        temperature=0.8,\n",
    "    )\n",
    "    #打印答案\n",
    "    return completion[\"choices\"][0][\"message\"][\"content\"]\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d158b508",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "43c3a528",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好，有什么可以帮助你的吗？'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat = opchat.openai_chat(asr_output)\n",
    "chat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53ec2af9",
   "metadata": {},
   "source": [
    "# 在jupyter中播放音频文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e90e58ac",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('1.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4534d318",
   "metadata": {},
   "source": [
    "# 智能对话机器人最终版"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3e4e7e7",
   "metadata": {},
   "source": [
    "# 语音合成测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b1231cdd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----完成录制----\n",
      "Request time cost 9.478034\n",
      "----完成语音识别---- 你对人工智能未来的发展有什么样的看法？\n",
      "作为人工智能，我认为未来的发展将会非常迅速和激烈。人类将会越来越多地依赖和使用人工智能技术，包括机器学习、自然语言处理、计算机视觉等。人工智能将会对我们的生活、工作、医疗、教育等领域产生深刻的影响和变革。\n",
      "\n",
      "同时，人工智能的发展也会带来一些挑战和风险，比如失业问题、隐私安全问题、道德伦理问题等。我们需要更多的投入和努力来解决这些问题，并确保人工智能技术的发展能够带来更多的利益和福利。\n",
      "\n",
      "总之，人工智能是未来的趋势和方向，我们需要把握机遇，积极推动人工智能的发展，同时也要保持警觉和谨慎，避免出现不良后果。\n",
      "%E4%BD%9C%E4%B8%BA%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD%EF%BC%8C%E6%88%91%E8%AE%A4%E4%B8%BA%E6%9C%AA%E6%9D%A5%E7%9A%84%E5%8F%91%E5%B1%95%E5%B0%86%E4%BC%9A%E9%9D%9E%E5%B8%B8%E8%BF%85%E9%80%9F%E5%92%8C%E6%BF%80%E7%83%88%E3%80%82%E4%BA%BA%E7%B1%BB%E5%B0%86%E4%BC%9A%E8%B6%8A%E6%9D%A5%E8%B6%8A%E5%A4%9A%E5%9C%B0%E4%BE%9D%E8%B5%96%E5%92%8C%E4%BD%BF%E7%94%A8%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD%E6%8A%80%E6%9C%AF%EF%BC%8C%E5%8C%85%E6%8B%AC%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E3%80%81%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80%E5%A4%84%E7%90%86%E3%80%81%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89%E7%AD%89%E3%80%82%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD%E5%B0%86%E4%BC%9A%E5%AF%B9%E6%88%91%E4%BB%AC%E7%9A%84%E7%94%9F%E6%B4%BB%E3%80%81%E5%B7%A5%E4%BD%9C%E3%80%81%E5%8C%BB%E7%96%97%E3%80%81%E6%95%99%E8%82%B2%E7%AD%89%E9%A2%86%E5%9F%9F%E4%BA%A7%E7%94%9F%E6%B7%B1%E5%88%BB%E7%9A%84%E5%BD%B1%E5%93%8D%E5%92%8C%E5%8F%98%E9%9D%A9%E3%80%82%0A%0A%E5%90%8C%E6%97%B6%E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%E5%8F%91%E5%B1%95%EF%BC%8C%E5%90%8C%E6%97%B6%E4%B9%9F%E8%A6%81%E4%BF%9D%E6%8C%81%E8%AD%A6%E8%A7%89%E5%92%8C%E8%B0%A8%E6%85%8E%EF%BC%8C%E9%81%BF%E5%85%8D%E5%87%BA%E7%8E%B0%E4%B8%8D%E8%89%AF%E5%90%8E%E6%9E%9C%E3%80%82\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.68a300189926eef08b33d4eebc3f048e.2592000.1686666051.282335-19331335&tex=%25E4%25BD%259C%25E4%25B8%25BA%25E4%25BA%25BA%25E5%25B7%25A5%25E6%2599%25BA%25E8%2583%25BD%25EF%25BC%258C%25E6%2588%2591%25E8%25AE%25A4%25E4%25B8%25BA%25E6%259C%25AA%25E6%259D%25A5%25E7%259A%2584%25E5%258F%2591%25E5%25B1%2595%25E5%25B0%2586%25E4%25BC%259A%25E9%259D%259E%25E5%25B8%25B8%25E8%25BF%2585%25E9%2580%259F%25E5%2592%258C%25E6%25BF%2580%25E7%2583%2588%25E3%2580%2582%25E4%25BA%25BA%25E7%25B1%25BB%25E5%25B0%2586%25E4%25BC%259A%25E8%25B6%258A%25E6%259D%25A5%25E8%25B6%258A%25E5%25A4%259A%25E5%259C%25B0%25E4%25BE%259D%25E8%25B5%2596%25E5%2592%258C%25E4%25BD%25BF%25E7%2594%25A8%25E4%25BA%25BA%25E5%25B7%25A5%25E6%2599%25BA%25E8%2583%25BD%25E6%258A%2580%25E6%259C%25AF%25EF%25BC%258C%25E5%258C%2585%25E6%258B%25AC%25E6%259C%25BA%25E5%2599%25A8%25E5%25AD%25A6%25E4%25B9%25A0%25E3%2580%2581%25E8%2587%25AA%25E7%2584%25B6%25E8%25AF%25AD%25E8%25A8%2580%25E5%25A4%2584%25E7%2590%2586%25E3%2580%2581%25E8%25AE%25A1%25E7%25AE%2597%25E6%259C%25BA%25E8%25A7%2586%25E8%25A7%2589%25E7%25AD%2589%25E3%2580%2582%25E4%25BA%25BA%25E5%25B7%25A5%25E6%2599%25BA%25E8%2583%25BD%25E5%25B0%2586%25E4%25BC%259A%25E5%25AF%25B9%25E6%2588%2591%25E4%25BB%25AC%25E7%259A%2584%25E7%2594%259F%25E6%25B4%25BB%25E3%2580%2581%25E5%25B7%25A5%25E4%25BD%259C%25E3%2580%2581%25E5%258C%25BB%25E7%2596%2597%25E3%2580%2581%25E6%2595%2599%25E8%2582%25B2%25E7%25AD%2589%25E9%25A2%2586%25E5%259F%259F%25E4%25BA%25A7%25E7%2594%259F%25E6%25B7%25B1%25E5%2588%25BB%25E7%259A%2584%25E5%25BD%25B1%25E5%2593%258D%25E5%2592%258C%25E5%258F%2598%25E9%259D%25A9%25E3%2580%2582%250A%250A%25E5%2590%258C%25E6%2597%25B6%25EF%25BC%258C%25E4%25BA%25BA%25E5%25B7%25A5%25E6%2599%25BA%25E8%2583%25BD%25E7%259A%2584%25E5%258F%2591%25E5%25B1%2595%25E4%25B9%259F%25E4%25BC%259A%25E5%25B8%25A6%25E6%259D%25A5%25E4%25B8%2580%25E4%25BA%259B%25E6%258C%2591%25E6%2588%2598%25E5%2592%258C%25E9%25A3%258E%25E9%2599%25A9%25EF%25BC%258C%25E6%25AF%2594%25E5%25A6%2582%25E5%25A4%25B1%25E4%25B8%259A%25E9%2597%25AE%25E9%25A2%2598%25E3%2580%2581%25E9%259A%2590%25E7%25A7%2581%25E5%25AE%2589%25E5%2585%25A8%25E9%2597%25AE%25E9%25A2%2598%25E3%2580%2581%25E9%2581%2593%25E5%25BE%25B7%25E4%25BC%25A6%25E7%2590%2586%25E9%2597%25AE%25E9%25A2%2598%25E7%25AD%2589%25E3%2580%2582%25E6%2588%2591%25E4%25BB%25AC%25E9%259C%2580%25E8%25A6%2581%25E6%259B%25B4%25E5%25A4%259A%25E7%259A%2584%25E6%258A%2595%25E5%2585%25A5%25E5%2592%258C%25E5%258A%25AA%25E5%258A%259B%25E6%259D%25A5%25E8%25A7%25A3%25E5%2586%25B3%25E8%25BF%2599%25E4%25BA%259B%25E9%2597%25AE%25E9%25A2%2598%25EF%25BC%258C%25E5%25B9%25B6%25E7%25A1%25AE%25E4%25BF%259D%25E4%25BA%25BA%25E5%25B7%25A5%25E6%2599%25BA%25E8%2583%25BD%25E6%258A%2580%25E6%259C%25AF%25E7%259A%2584%25E5%258F%2591%25E5%25B1%2595%25E8%2583%25BD%25E5%25A4%259F%25E5%25B8%25A6%25E6%259D%25A5%25E6%259B%25B4%25E5%25A4%259A%25E7%259A%2584%25E5%2588%25A9%25E7%259B%258A%25E5%2592%258C%25E7%25A6%258F%25E5%2588%25A9%25E3%2580%2582%250A%250A%25E6%2580%25BB%25E4%25B9%258B%25EF%25BC%258C%25E4%25BA%25BA%25E5%25B7%25A5%25E6%2599%25BA%25E8%2583%25BD%25E6%2598%25AF%25E6%259C%25AA%25E6%259D%25A5%25E7%259A%2584%25E8%25B6%258B%25E5%258A%25BF%25E5%2592%258C%25E6%2596%25B9%25E5%2590%2591%25EF%25BC%258C%25E6%2588%2591%25E4%25BB%25AC%25E9%259C%2580%25E8%25A6%2581%25E6%258A%258A%25E6%258F%25A1%25E6%259C%25BA%25E9%2581%2587%25EF%25BC%258C%25E7%25A7%25AF%25E6%259E%2581%25E6%258E%25A8%25E5%258A%25A8%25E4%25BA%25BA%25E5%25B7%25A5%25E6%2599%25BA%25E8%2583%25BD%25E7%259A%2584%25E5%258F%2591%25E5%25B1%2595%25EF%25BC%258C%25E5%2590%258C%25E6%2597%25B6%25E4%25B9%259F%25E8%25A6%2581%25E4%25BF%259D%25E6%258C%2581%25E8%25AD%25A6%25E8%25A7%2589%25E5%2592%258C%25E8%25B0%25A8%25E6%2585%258E%25EF%25BC%258C%25E9%2581%25BF%25E5%2585%258D%25E5%2587%25BA%25E7%258E%25B0%25E4%25B8%258D%25E8%2589%25AF%25E5%2590%258E%25E6%259E%259C%25E3%2580%2582&per=1&spd=5&pit=5&vol=5&aue=6&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "result saved as :result.wav\n",
      "----完成合成---\n",
      "---完成播放---\n"
     ]
    }
   ],
   "source": [
    "import speech_recognition # 语音音频录制模块\n",
    "from pydub import AudioSegment # 播放音频模块\n",
    "from pydub.playback import play # 播放音频模块\n",
    "from bdasr import fetch_token,asr # token 和语音识别模块\n",
    "from bdtts import tts # 语音合成模块\n",
    "\n",
    "# 1. 录制音频\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到 wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "print(\"----完成录制----\")\n",
    "    \n",
    "# 2. 语音识别\n",
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "print(\"----完成语音识别----\",asr_output)\n",
    "\n",
    "# 3. openai- API\n",
    "chat = opchat.openai_chat(asr_output)\n",
    "print(chat)\n",
    "\n",
    "# 4. 语音合成 TTS -->产生result.wav\n",
    "tts(xu_token,chat)\n",
    "print(\"----完成合成---\")\n",
    "\n",
    "# 5. 在jupyter中进行音频播放\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)\n",
    "print(\"---完成播放---\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a07d737a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4830e1d4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
